﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WF_QLThuVien.Utilities;
using WF_QLThuVien.BusinessLayer;
using WF_QLThuVien.Entities;

namespace WF_QLThuVien.PresentationLayer
{
    public partial class FormLogIn : Form
    {
        CBLL_Login bllLogin = new CBLL_Login();

        public delegate void OpenFormQuanLy(bool flag);
        public event OpenFormQuanLy open;

        public FormLogIn()
        {
            InitializeComponent();
            open += new OpenFormQuanLy(FormLogIn_open);
        }

        void FormLogIn_open(bool flag)
        {
            
        }

        private void FormLogIn_Load(object sender, EventArgs e)
        {
            btnOk.Enabled = false;
            txtPass.UseSystemPasswordChar = true;
            txtPass.PasswordChar = '>';
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (!CKiemTra.IsNull(txtUser, "Chưa nhập User!", errDangNhap) && !CKiemTra.IsNull(txtPass, "Chưa nhập Password!", errDangNhap))
            {
                int n = bllLogin.CheckUser(new CLogin(txtUser.Text, txtPass.Text, "123"));
                if (n == 1)
                {                    
                    this.Dispose();
                    open(true);
                }
                else if (n == 2)
                {
                    MessageBox.Show("Bạn quên gì đó chăng?");
                    open(false);
                }
                else
                {
                    MessageBox.Show("Database trống!");
                    open(false);
                }
            }
        }

        private void txtUser_TextChanged(object sender, EventArgs e)
        {
            if (CKiemTra.IsNull(txtUser, "Nhập user name!", errDangNhap))
            {
                btnOk.Enabled = false;
            }
            else
            {
                btnOk.Enabled = true;
            }
        }

        private void txtUser_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Tab)
            {
                if (!CKiemTra.IsNull(txtUser, "Nhập user name", errDangNhap))
                {
                    txtPass.Focus();
                }
            }
        }

        private void txtUser_KeyPress(object sender, KeyPressEventArgs e)
        {
            
        }

        private void txtPass_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Tab)
            {
                if (!CKiemTra.IsNull(txtPass, "Nhập password", errDangNhap))
                {
                    btnOk_Click(sender, e);
                }
            }
        }

        private void FormLogIn_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                this.Dispose();
            }
        }
    }
}
